﻿@using LibraryCatalog.Models
@using PropertyExpression.Common
@model EditBookModel
<div class="editBookTitle">Редактирование</div>
<form id="@Const.editBookForm" data-bookid="@Model.Book.Match(_ => _.BookId.ToString(), () => "")">
    <table class="editTable">
        <tr>
            <td style="width: 120px;">Название книги</td>
            <td>
                <input id="@Const.bookName" type="text" class="editInput"
                       value="@Model.Request.Match(_ => _.Name,
                           () => Model.Book.Select(_ => _.Name).ValueOrDefault())" />
                @if (Model.Request.Select(_ => _.NameIsEmpty).ValueOrDefault()) {
                    <span class="@Const.errorMessage">Введите название книги</span>
                }
            </td>
        </tr>
        <tr>
            <td>Автор</td>
            <td>
                <input id="@Const.bookAuthor" type="text" class="editInput"
                       value="@Model.Request.Match(_ => _.Author,
                           () => Model.Book.Select(_ => _.Author).ValueOrDefault())" />
                @if (Model.Request.Select(_ => _.AuthorIsEmpty).ValueOrDefault()) {
                    <span class="@Const.errorMessage">Введите автора</span>
                }
            </td>
        </tr>
        <tr>
            <td>ISBN</td>
            <td>
                <input id="@Const.Isbn" type="text" class="editInput"
                       value="@Model.Request.Match(_ => _.Isbn,
                           () => Model.Book.Select(_ => _.Isbn).ValueOrDefault())" />
            </td>
        </tr>
        <tr>
            <td>Библиотека</td>
            <td>
                <select id="@Const.LibraryId" class="editInput">
                    @{
                        var value = Model.Request.Match(_ => _.LibraryId,
                            () => Model.Book.Select(_ => _.LibraryId.ToString()).ValueOrDefault());
                    }
                    <option value="" @Html.Raw(string.IsNullOrEmpty(value) ? "selected" : "")></option>
                    @foreach (var library in Model.Libraries) {
                        <option @Html.Raw(value == library.LibraryId.ToString() ? "selected" : "")
                                value="@library.LibraryId">
                            @library.Name
                        </option>
                    }
                </select>
                @if (Model.Request.Select(_ => _.LibraryIdIsEmpty).ValueOrDefault()) {
                    <span class="@Const.errorMessage">Выберите библиотеку</span>
                }
            </td>
        </tr>
    </table>
    <input type="button" id="@Const.cancelButton" value="Отмена" class="dialogButton rightDialogButton" />
    <input type="submit" value="Сохранить" class="dialogButton" />
    <div style="clear: both"></div>
</form>
<script>
    $(function () {
        $('#@Const.cancelButton').click(closeDialog);
        $("#@Const.editBookForm").submit(function (event) {
            event.preventDefault();
            $.ajax({
                url: '@(Url.Action("SaveBook"))',
                data: {
                    @{ var book = new Type<BookRequest>(); }
                    '@book.Name(_ => _.BookId)': $("#@Const.editBookForm").attr('data-bookid'),
                    '@book.Name(_ => _.Name)': $('#@Const.bookName').val(),
                    '@book.Name(_ => _.Author)': $('#@Const.bookAuthor').val(),
                    '@book.Name(_ => _.Isbn)': $('#@Const.Isbn').val(),
                    '@book.Name(_ => _.LibraryId)': $('#@Const.LibraryId').val()
                },
                success: function(data) {
                    var regExp = new RegExp('^\\s*@Const.Close', 'g');
                    var match = regExp.exec(data);
                    if (match) {
                        $('#@Const.BooksGridContainer').html(data.substring(regExp.lastIndex));
                        closeDialog();
                    } else {
                        $('#@Const.editBookDialogContent').html(data);
                        $('.@Const.errorMessage:first').parent().find('input,select').focus();
                    }
                }
            });
        });
    });
</script>